package com.lucene;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import net.paoding.analysis.analyzer.PaodingAnalyzer;


public class LuceneTest {

	public static void main(String[] args) throws Exception {
		/* 指明要索引文件夹的位置 */
		File fileDir = new File("D:\\luceneData");

		/* 这里放索引文件的位置 */
		String indexPath = "D:\\index";

		Directory dir = FSDirectory.open(Paths.get(indexPath));

		//Analyzer analyzer = new StandardAnalyzer();
		SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer();
		//Analyzer analyzer2 = new PaodingAnalyzer();
	
		IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
		iwc.setOpenMode(OpenMode.CREATE);
		IndexWriter indexWriter = new IndexWriter(dir, iwc);
		File[] textFiles = fileDir.listFiles();
		long startTime = new Date().getTime();

		// 增加document到索引去
		for (int i = 0; i < textFiles.length; i++) {
			if (textFiles[i].isFile()) {
				System.out.println("File " + textFiles[i].getCanonicalPath() + " 正在被索引....");
				textFiles[i].getName();
				System.out.println("文件名：" + textFiles[i].getName());
				String temp = FileReaderAll(textFiles[i].getCanonicalPath(), "GBK");
				System.out.println("文件的内容：" + temp);

				long time = textFiles[i].lastModified();
				String modifitime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time));
				System.out.println("文件的日期：" + modifitime);

				Document document = new Document();
				//文件名能被索引、存储
				Field FieldName = new StringField("name", textFiles[i].getName(), Field.Store.YES);
				//Field FieldName = new Field("name", textFiles[i].getName(), FieldName_fieldType);
				//内容能被索引，不存储
				Field FieldBody = new TextField("body", temp, Field.Store.YES);
				//修改时间存储，不被检索
				Field modifiField = new StoredField("modifiField", time);
				
				document.add(FieldName);
				document.add(FieldBody);
				document.add(modifiField);
				document.add(new NumericDocValuesField("modifiField", time));
				document.add(new IntPoint("modifiField", (int) time));

				System.out.println("**********");
				indexWriter.addDocument(document);
			}
		}
		indexWriter.close();

		// 测试一下索引的时间
		long endTime = new Date().getTime();
		System.out.println("这占用了" + (endTime - startTime) + " 毫秒来把文档增加到索引里面去!" + fileDir.getPath());
	}

	public static String FileReaderAll(String FileName, String charset) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(FileName), charset));
		String line = new String();
		String temp = new String();

		while ((line = reader.readLine()) != null) {
			temp += line;
		}
		reader.close();
		return temp;
	}

}
